package team.niit.service.impl;

import cn.hutool.crypto.SecureUtil;
import team.niit.dao.AdminDAO;
import team.niit.dao.StudentDAO;
import team.niit.dao.impl.AdminDAOImpl;
import team.niit.dao.impl.StudentDAOImpl;
import team.niit.service.UserService;

/**
 * @Author: 然然然儿呐_
 * @PackageName team.niit.service.impl
 * @Description: TO DO
 * @Date 2021/11/28 15:17
 */
public class UserServiceImpl implements UserService {
    private StudentDAO studentDAO = new StudentDAOImpl();
    private AdminDAO adminDAO = new AdminDAOImpl();

    @Override
    public boolean modifyPassword(String id, String oldPassword, String newPassword) {
        String oldMD5Password = SecureUtil.md5(oldPassword);
        String newMD5Password = SecureUtil.md5(newPassword);
        if (id.length() == 11) {
            return studentDAO.modifyPassword(id,oldMD5Password,newMD5Password);
        } else {
            return adminDAO.modifyPassword(id,oldMD5Password,newMD5Password);
        }
    }

    @Override
    public Object login(String id, String password) {
        String MD5Password = SecureUtil.md5(password);
        if (id.length() == 11) {
            return studentDAO.login(id,MD5Password);
        } else {
            return adminDAO.login(id,MD5Password);
        }
    }

    @Override
    public boolean retrievePassword(String id, String password, String phone) {
        String MD5Password = SecureUtil.md5(password);
        if (id.length() == 11) {
            return studentDAO.retrievePassword(id,MD5Password,phone);
        } else {
            return adminDAO.retrievePassword(id,MD5Password,phone);
        }
    }

    @Override
    public boolean initPassword(String id, String password) {
        String MD5Password = SecureUtil.md5(password);
        return studentDAO.initPassword(id,MD5Password);
    }
}
